package com.clps.algorithm03.链表;

public class Stack1 <E>{

    private class LinkNode{
        E val; //节点中存储的数据
        LinkNode next;//指向下一个节点的指针。

        public LinkNode(E val, LinkNode next) {
            this.val = val;
            this.next = next;
        }
    }

    private  LinkNode head;//栈顶节点，无论添加还是删除，始终是变动的
    //往栈中添加元素

    public void  push(E val){
        //把数据放入栈顶,如果head为空会创建head节点
        head = new LinkNode(val,head);
    }
    
    public E pop(){
        if(head == null){
            throw  new NullPointerException();
        }
        
        LinkNode next = head.next;
        E result = head.val;
        head.val = null;
        head = next;
        return  result;
    }


    public  E top(){
        if(head == null){
            throw  new NullPointerException();
        }
        return  head.val;
    }


}
